from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.InterviewAdminController import InterviewAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import QueryBase, InterviewQuery
from schema.TeachingSchema import InterviewInfo, InterviewContentInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

interview_admin_router = APIRouter()


@interview_admin_router.post("/interview/add", summary='添加面试信息')
def add_interview(interviewInfo: InterviewInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interview/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/add', db):
            response = InterviewAdminController.add_interview(interviewInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.post("/interview/mod", summary='根据面试信息id修改面试信息')
def mod_interview(interviewInfo: InterviewInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interview/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/mod', db):
            response = InterviewAdminController.mod_interview(interviewInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.post("/interview/querybypage", summary='分页查询所有面试信息')
def query_interview_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interview/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/query', db):
            response = InterviewAdminController.query_interview_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.post("/interview/query", summary='根据学员id分页查询面试信息')
def query_interview_by_field(interviewQuery: InterviewQuery, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interview/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/query', db):
            response = InterviewAdminController.query_interview_by_field(interviewQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.post("/interviewcontent/add", summary='添加面试内容信息')
def add_interview_content(interviewContentInfo: InterviewContentInfo, db: Session = Depends(get_db_session),
                          result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interviewcontent/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/add', db):
            response = InterviewAdminController.add_interview_content(interviewContentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.post("/interviewcontent/mod", summary='根据面试内容信息id修改面试内容信息')
def mod_interview_content(interviewContentInfo: InterviewContentInfo, db: Session = Depends(get_db_session),
                          result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interviewcontent/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/mod', db):
            response = InterviewAdminController.mod_interview_content(interviewContentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@interview_admin_router.get("/interviewcontent/query/{interview_id}", summary='根据面试信息id查询面试内容信息')
def query_interview_content_by_field(interview_id: int, db: Session = Depends(get_db_session),
                                     result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/interviewcontent/query/interview_id={interview_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/interview/query', db):
            response = InterviewAdminController.query_interview_content_by_field(interview_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
